
/* 定义全局数据存取对象*/
var attribute={};
attribute.usrVal='';
attribute.usrInfo=false;
attribute.pwdInfo=false;
/* 入口函数*/
$(function(){
	$('#register').click(registerAction);
	init();
});

/* 登录界面初始化逻辑*/
function init(){
	$('#backLogin').click(backLogin);
	
	$('#usr').focus(usrFocus);
	$('#usr').blur(usrBlur);
	$('#pwd').focus(pwdFocus);
	$('#pwd').blur(pwdBlur);
	$('#login').click(loginAction);
}

/*注册页面初始化*/
function registerAction(){
	attributeInit();
	attribute.usrVal='';
	
	$('#h1Title').html('用户注册');
	var html='<input id="usr" type="text" value="用户名">'
		+'<lable>密码&重复</lable>'
		+'<input id="pwd" type="password" ">'
		+'<input id="pwdto" type="password">';
	
	$('#form').html(html);
	$('#form lable').css({
		"text-align":"center",
		"color":"#F45B4B",
		"margin":"0px 0px 10px 0px"
	});
	
	
	html='<input id="back" type="button" value="返回"><input id="rebut" type="button" value="注册">';
	$('#interface').html(html);
	$('#back').click(registerBack);
	registerInit();
	
}

/* 注册页面初始化逻辑*/
function registerInit(){
	$('#usr').focus(usrFocus);
	$('#usr').blur(usrBlur);
	$('#pwd').focus(pwdFocus);
	$('#pwd').blur(pwdBlur);
	$('#pwdto').focus(pwdtoFocus);
	
	$('#rebut').click(register);
}

/*pwdto获取焦点事件*/
function pwdtoFocus(){
	$('#pwdto').css('border-color','');
	this.value='';
}
/*注册提交数据逻辑校验*/
function register(){
	console.log("11111");
	if(attribute.usrInfo+attribute.pwdInfo!=2){
		return;
	}
	var pwd=$('#pwd').val();
	var pwdto=$('#pwdto').val();
	if(pwd!=pwdto){
		$('#pwdto').css('border-color','#F45B4B');
		return;
	}
	/*提交逻辑*/
	registerSubmit();
	
	
}

/* 注册界面返回按钮逻辑功能*/
function registerBack(){
	attributeInit();
	attribute.usrVal='';
	
	$('#h1Title').html('用户登录');
	var html='<input id="usr" type="text" value="用户名">'
		+'<input id="pwd" type="password" value="password">';
	$('#form').html(html);
	
	html='<input id="backLogin" type="button" value="返回"><input id="login" type="button" value="登录">';
	$('#interface').html(html);
	init();
}
/* 登录界面返回按钮逻辑功能*/
function backLogin(){
	location.href = "index.html";
}

/* usr获取焦点事件*/
function usrFocus(){
	var usr=$('#usr').val();
	if(usr == '用户名'){
		this.value='';
	}
	$('#usr').css('border-color','');
	this.value=attribute.usrVal;
}
/* usr失去焦点事件*/
function usrBlur(){
	var usr=$('#usr').val();
	attribute.usrVal=usr;
	
	if(usr==''){
		//console.log("123");
		this.value='用户名';
	}
	var reg=/^\w{3,10}$/;
	if(reg.test(usr)){
		attribute.usrInfo=true;
	}else{
		$('#usr').css('border-color','#F45B4B');
		this.value='3~10个数组、字母';
		//console.log(attribute.usrVal);
		attribute.usrInfo=false;
	}
	
}

/* pwd获取焦点事件*/
function pwdFocus(){
	this.type='password';
	this.value='';
	$('#pwd').css('border-color','');
	var usr=$('#usr').val();
	if(attribute.usrVal !=''){
		$('#usr').val(attribute.usrVal);
		$('#usr').css('border-color','');
	}
	
}
/* pwd失去焦点事件*/
function pwdBlur(){
	var pwd=$('#pwd').val();
	//console.log(pwd);
	var reg=/^\w{3,10}$/;
	if(reg.test(pwd)){
		attribute.pwdInfo=true;
	}else{
		this.type='text';
		$('#pwd').css('border-color','#F45B4B');
		this.value='3~10个数组、字母';
		attribute.pwdInfo=false;
	}
}

/*登录提交数据校验*/
function loginAction(){
	/*2017/01/10
	 * 明天重写这部分
	 * */
	//console.log('loginAction');
	if(attribute.usrInfo+attribute.pwdInfo!=2){
		//console.log("off");
		attributeInit();
		return;
	}
	attributeInit();
	//console.log('ok');
	/*后面是表单提交逻辑*/
	loginSubmit();
	
	
}
/* 重用数据块*/
function attributeInit(){
	attribute.usrInfo=false;
	attribute.pwdInfo=false;

}

/*登录提交表单*/
function loginSubmit(){
	var account=$('#usr').val();
	var password=$('#pwd').val();
	//console.log(account);
	//console.log(password);
	var url='user/login.do';
	var param={"account":account,"password":password};
	$.post(url,param,function(result){
		
		/*2017/01/12明天查找问题*/
		if(result.state==1){
			var user=result.data[0];
			//console.log(user);
			
			
			location.href='index.html';
		}else{
			$('#usr').css('border-color','#F45B4B');
			$('#pwd').css('border-color','#F45B4B');
			$('#usr').val('账号或密码错误');
		}
	});
}
/*注册提交表单*/
function registerSubmit(){
	var account =$('#usr').val();
	var password =$('#pwd').val();
	var pwdto=$('#pwdto').val();
	console.log(account);
	console.log(password);
	console.log(pwdto);
	var url="user/save.do";
	var param={"account":account,"password":password,"pwdto":pwdto};
	$.post(url,param,function(result){
		console.log(result);
		if(result.state==1){
			attribute.resultSize=1;
			console.log(result);
			registerBack();
		}else{
			$('#usr').css('border-color','#F45B4B');
			$('#pwd').css('border-color','#F45B4B');
			$('#pwdto').css('border-color','#F45B4B');
			$('#usr').val(result.message);
		}
		
	});
	
}




